#include "playersettingsdialog.h"

/**
 * @brief 构造函数
 * @param parent 父控件
 */
PlayerSettingsDialog::PlayerSettingsDialog(QWidget *parent)
    : QDialog(parent)
    , m_playModeComboBox(nullptr)
    , m_playbackRateComboBox(nullptr)
    , m_okButton(nullptr)
    , m_cancelButton(nullptr)
    , m_currentPlayMode(VideoPlayerModel::Sequential)
    , m_currentPlaybackRate(1.0)
{
    setupUI();
}

/**
 * @brief 析构函数
 */
PlayerSettingsDialog::~PlayerSettingsDialog()
{
}

/**
 * @brief 初始化UI
 */
void PlayerSettingsDialog::setupUI()
{
    setWindowTitle("播放器设置");
    setFixedSize(400, 250);
    
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setContentsMargins(20, 20, 20, 20);
    mainLayout->setSpacing(15);
    
    // 播放模式设置组
    QGroupBox *playModeGroup = new QGroupBox("播放模式", this);
    QVBoxLayout *playModeLayout = new QVBoxLayout(playModeGroup);
    
    m_playModeComboBox = new QComboBox(playModeGroup);
    m_playModeComboBox->addItem("顺序播放", VideoPlayerModel::Sequential);
    m_playModeComboBox->addItem("列表循环", VideoPlayerModel::Loop);
    m_playModeComboBox->addItem("单曲循环", VideoPlayerModel::CurrentLoop);
    m_playModeComboBox->addItem("随机播放", VideoPlayerModel::Random);
    m_playModeComboBox->setStyleSheet(
        "QComboBox { padding: 5px; font-size: 12px; border: 1px solid #bdc3c7; "
        "border-radius: 3px; background: white; } "
        "QComboBox::drop-down { border: none; } "
        "QComboBox::down-arrow { image: url(:/icons/arrow_down.png); width: 12px; height: 12px; }"
    );
    playModeLayout->addWidget(m_playModeComboBox);
    
    mainLayout->addWidget(playModeGroup);
    
    // 播放速度设置组
    QGroupBox *playbackRateGroup = new QGroupBox("播放速度", this);
    QVBoxLayout *playbackRateLayout = new QVBoxLayout(playbackRateGroup);
    
    m_playbackRateComboBox = new QComboBox(playbackRateGroup);
    m_playbackRateComboBox->addItem("0.5x (慢速)", 0.5);
    m_playbackRateComboBox->addItem("0.75x", 0.75);
    m_playbackRateComboBox->addItem("1.0x (正常)", 1.0);
    m_playbackRateComboBox->addItem("1.25x", 1.25);
    m_playbackRateComboBox->addItem("1.5x (快速)", 1.5);
    m_playbackRateComboBox->addItem("2.0x (极快)", 2.0);
    m_playbackRateComboBox->setCurrentIndex(2); // 默认1.0x
    m_playbackRateComboBox->setStyleSheet(
        "QComboBox { padding: 5px; font-size: 12px; border: 1px solid #bdc3c7; "
        "border-radius: 3px; background: white; } "
        "QComboBox::drop-down { border: none; } "
        "QComboBox::down-arrow { image: url(:/icons/arrow_down.png); width: 12px; height: 12px; }"
    );
    playbackRateLayout->addWidget(m_playbackRateComboBox);
    
    mainLayout->addWidget(playbackRateGroup);
    
    mainLayout->addStretch();
    
    // 按钮布局
    QHBoxLayout *buttonLayout = new QHBoxLayout();
    buttonLayout->setSpacing(10);
    
    m_okButton = new QPushButton("确定", this);
    m_okButton->setFixedSize(100, 40);
    m_okButton->setStyleSheet(
        "QPushButton { background-color: #27ae60; color: white; border: none; "
        "border-radius: 5px; font-size: 12px; font-weight: bold; } "
        "QPushButton:hover { background-color: #229954; } "
        "QPushButton:pressed { background-color: #1e8449; }"
    );
    
    m_cancelButton = new QPushButton("取消", this);
    m_cancelButton->setFixedSize(100, 40);
    m_cancelButton->setStyleSheet(
        "QPushButton { background-color: #95a5a6; color: white; border: none; "
        "border-radius: 5px; font-size: 12px; font-weight: bold; } "
        "QPushButton:hover { background-color: #7f8c8d; } "
        "QPushButton:pressed { background-color: #636e72; }"
    );
    
    buttonLayout->addStretch();
    buttonLayout->addWidget(m_okButton);
    buttonLayout->addWidget(m_cancelButton);
    
    mainLayout->addLayout(buttonLayout);
    
    // 连接信号槽
    connect(m_okButton, &QPushButton::clicked, this, &PlayerSettingsDialog::onOkClicked);
    connect(m_cancelButton, &QPushButton::clicked, this, &PlayerSettingsDialog::onCancelClicked);
    
    // 设置对话框样式
    setStyleSheet(
        "QDialog { background-color: #ecf0f1; } "
        "QGroupBox { font-size: 13px; font-weight: bold; color: #2c3e50; "
        "border: 2px solid #bdc3c7; border-radius: 5px; margin-top: 10px; padding-top: 10px; } "
        "QGroupBox::title { subcontrol-origin: margin; left: 10px; padding: 0 5px; }"
    );
}

/**
 * @brief 设置当前播放模式
 */
void PlayerSettingsDialog::setPlayMode(VideoPlayerModel::PlayMode mode)
{
    m_currentPlayMode = mode;
    
    // 在下拉框中选中对应项
    for (int i = 0; i < m_playModeComboBox->count(); ++i) {
        if (m_playModeComboBox->itemData(i).toInt() == static_cast<int>(mode)) {
            m_playModeComboBox->setCurrentIndex(i);
            break;
        }
    }
}

/**
 * @brief 设置当前播放速度
 */
void PlayerSettingsDialog::setPlaybackRate(qreal rate)
{
    m_currentPlaybackRate = rate;
    
    // 在下拉框中选中对应项
    for (int i = 0; i < m_playbackRateComboBox->count(); ++i) {
        if (qAbs(m_playbackRateComboBox->itemData(i).toReal() - rate) < 0.01) {
            m_playbackRateComboBox->setCurrentIndex(i);
            break;
        }
    }
}

/**
 * @brief 获取选择的播放模式
 */
VideoPlayerModel::PlayMode PlayerSettingsDialog::playMode() const
{
    int modeValue = m_playModeComboBox->currentData().toInt();
    return static_cast<VideoPlayerModel::PlayMode>(modeValue);
}

/**
 * @brief 获取选择的播放速度
 */
qreal PlayerSettingsDialog::playbackRate() const
{
    return m_playbackRateComboBox->currentData().toReal();
}

/**
 * @brief 确定按钮点击
 */
void PlayerSettingsDialog::onOkClicked()
{
    // 发送设置已应用信号
    emit settingsApplied(playMode(), playbackRate());
    
    // 关闭对话框
    accept();
}

/**
 * @brief 取消按钮点击
 */
void PlayerSettingsDialog::onCancelClicked()
{
    // 关闭对话框，不保存设置
    reject();
}

